from src.Dao.PersonDao import PersonDao
from src.Utils.index import ResponseService,colored_text
import json

personDB = PersonDao()

class PersonService:
    
    def __init__(self):
        pass
    
    def login(self,phone: str):
        if type(phone) is not str or len(phone) != 11:
            print(colored_text("手机号非法","red"))
        else:
            print("手机号为："+phone)
            res = personDB.login(phone)
            if not res:  # 没有登录过，直接注册
                result = personDB.register(phone)
                print("注册的信息是:")
                print(result)
                if not result:
                    return ResponseService.error(None,'注册失败!')
                else:
                    return ResponseService.success(result,'注册并登录成功')
            else:
                return ResponseService.success(res,'登录成功')
            
    def getOrder(self,userId: int):
        if type(userId) is not int or not userId:
            print(colored_text("用户id非法","red"))
        else:
            result = personDB.getOrder(userId=userId)
            if not result:
                return ResponseService.error(None,"查询购物车失败")
            else:
                return ResponseService.success(json.loads(result),"查询购物车成功")
            
    def updateOrder(self,userId: int,info):
        if type(userId) is not int or not userId:
            print(colored_text("用户id非法","red"))
        else:
            res = personDB.updateOrder(userId,info)
            if not res:
                return ResponseService.error(None,"更新购物车失败")
            else:
                return ResponseService.success(res,"更新购物车成功")